//
//  MBModelDetailFooterView.m
//  Meboo
//
//  Created by kyo on 2024/3/11.
//

#import "MBModelDetailFooterView.h"
#import "MBConversationViewController.h"
#import "KLVipViewController.h"
#import <TYCyclePagerView/TYCyclePagerView.h>
#import "MBFooterBannerCell.h"
#import "MBFooterPageView.h"
#import "GKPhotoBrowser.h"
//#import "YBImageBrowser.h"
@interface MBModelDetailFooterView ()
<
TYCyclePagerViewDelegate,
TYCyclePagerViewDataSource
>
@property (nonatomic , strong) TYCyclePagerView *cycleScrollView;
@property (nonatomic, strong) MBFooterPageView *pageControlView;
@property(nonatomic,strong) UIButton *messageBtn;

@property (nonatomic, strong) UIView *authorView;
@property(nonatomic,strong) UILabel *nameLab;
@property(nonatomic,strong) UILabel *interestsLab;

@property(nonatomic,strong) NSMutableArray *dataArr;
@end

@implementation MBModelDetailFooterView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}


#pragma mark - Public Metohd
- (void)setModel:(MBWLMTopicModel *)model {
    _model = model;
    self.pageControlView.totalNum = model.images.count;
    self.pageControlView.index = 0;
    
    [self.pageControlView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(MBHeightScale(6)*self.model.images.count-MBHeightScale(3));
    }];
    
    MBWLMTopicImageModel *imageModel = model.images[0];

    self.nameLab.text = [NSString stringWithFormat:@"%@,%@",model.nickName,model.age];
    
    NSString *interestsStr = @"";
    for (MBWLMTopicImageModel *intModel in model.interests) {
        if (interestsStr.length > 0) {
            interestsStr = [NSString stringWithFormat:@"%@, %@",interestsStr,intModel.interest];
        } else {
            interestsStr = [NSString stringWithFormat:@"%@%@",interestsStr,intModel.interest];
        }
    }
    self.interestsLab.text = interestsStr;
    
    [self.cycleScrollView reloadData];
}

#pragma mark - Action
- (void)messageAction {
    if ([MBUserAccount defaultAccount].isVip) {
        MBConversationViewController *conversationVC = [[MBConversationViewController alloc] initWithConversationType:ConversationType_PRIVATE targetId:self.model.friendUserCode];
        [self.currentViewController.navigationController pushViewController:conversationVC animated:YES];
    } else {
        [self.currentViewController.navigationController pushViewController:[KLVipViewController new] animated:YES];
    }
    
}

- (void)backAction {
    [self.currentViewController.navigationController popViewControllerAnimated:YES];
}

#pragma mark - TYCyclePagerViewDataSource
- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return self.model.images.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    
    MBFooterBannerCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"MBFooterBannerCell" forIndex:index];
    NSString *urlStr = self.model.images[index].imageUrl;
    cell.imageUrl = urlStr;
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.itemSize = CGSizeMake(MBHeightScale(120), MBHeightScale(120));
    layout.itemSpacing = MBWidthScale(0);
    //layout.minimumAlpha = 0.3;
//    layout.itemHorizontalCenter = _horCenterSwitch.isOn;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    self.pageControlView.index = toIndex;
}

#pragma mark -- TYCyclePagerViewDelegate
- (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(__kindof UICollectionViewCell *)cell atIndex:(NSInteger)index {
    MBFooterBannerCell *cell0 = cell;
    [self.dataArr removeAllObjects];
    for (MBWLMTopicImageModel *imageModel1 in self.model.images) {
        GKPhoto *data = [GKPhoto new];
        data.url = [NSURL URLWithString:imageModel1.imageUrl];
        data.sourceImageView = cell0.imageView;
        data.placeholderImage = ImageWithName(@"appLogo");
        [self.dataArr addObject:data];
    }
//    [self.dataArr removeAllObjects];
//    for (MBWLMTopicImageModel *imageModel1 in self.model.images) {
//        YBIBImageData *data = [YBIBImageData new];
//        data.imageURL = [NSURL URLWithString:imageModel1.imageUrl];
//        data.projectiveView = cell0.imageView;
//        data.thumbImage = ImageWithName(@"appLogo");
//        [self.dataArr addObject:data];
//    }
//    
//    YBImageBrowser *browser = [YBImageBrowser new];
//    browser.currentPage = index;
//    browser.dataSourceArray = self.dataArr;
//    [browser show];
    GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:self.dataArr currentIndex:index];
    [browser showFromVC:self.currentViewController];
}

#pragma mark - UI
- (void)setupUI {
    self.backgroundColor = UIColor.clearColor;
    
    [self addSubview:self.cycleScrollView];
    [self.cycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.left.mas_equalTo(MBWidthScale(20));
        make.width.mas_equalTo(MBHeightScale(120));
    }];
    
    [self addSubview:self.pageControlView];
    [self.pageControlView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.cycleScrollView);
        make.bottom.mas_equalTo(MBHeightScale(-4));
        make.height.mas_equalTo(MBHeightScale(3));
    }];
    
    [self addSubview:self.messageBtn];
    [self.messageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
        make.right.mas_equalTo(MBWidthScale(-20));
        make.left.equalTo(self.cycleScrollView.mas_right).offset(MBWidthScale(12));
        make.height.mas_equalTo(MBHeightScale(40));
    }];
    
    
    [self addSubview:self.authorView];
    [self.authorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.right.mas_equalTo(MBWidthScale(-20));
        make.left.equalTo(self.cycleScrollView.mas_right).offset(MBWidthScale(12));
        make.bottom.equalTo(self.messageBtn.mas_top).offset(MBHeightScale(-12));
    }];
    
    [self.authorView addSubview:self.nameLab];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(MBHeightScale(10));
        make.left.mas_equalTo(MBWidthScale(10));
        make.right.mas_equalTo(MBWidthScale(-10));
    }];
    
    [self.authorView addSubview:self.interestsLab];
    [self.interestsLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLab.mas_bottom).offset(MBHeightScale(5));
        make.left.mas_equalTo(MBWidthScale(10));
        make.right.mas_equalTo(MBWidthScale(-10));
    }];
}

#pragma mark - Setter && Getter
- (UIButton *)messageBtn {
    if (!_messageBtn) {
        _messageBtn = [[UIButton alloc] init];
        [_messageBtn setTitle:@"Contact the author" forState:0];
        _messageBtn.titleLabel.font = PDSemiBoldScale(14);
        [_messageBtn setTitleColor:UIColor.whiteColor forState:0];
        _messageBtn.layer.cornerRadius = MBHeightScale(6);
        [_messageBtn setBackgroundColor:HEXS(@"B92176")];
        [_messageBtn addTarget:self action:@selector(messageAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _messageBtn;
}

- (UILabel *)nameLab {
    if (!_nameLab) {
        _nameLab = [UILabel new];
        _nameLab.font = PDSemiBoldScale(14);
        _nameLab.textColor = HEXS(@"333333");
    }
    return _nameLab;
}

- (UILabel *)interestsLab {
    if (!_interestsLab) {
        _interestsLab = [UILabel new];
        _interestsLab.font = PDRegularScale(12);
        _interestsLab.textColor = HEXS(@"333333");
    }
    return _interestsLab;
}

- (TYCyclePagerView *)cycleScrollView {
    if (!_cycleScrollView) {
        _cycleScrollView = [[TYCyclePagerView alloc] init];
        _cycleScrollView.isInfiniteLoop = YES;
        _cycleScrollView.delegate = self;
        _cycleScrollView.dataSource = self;
        _cycleScrollView.backgroundColor = [UIColor clearColor];
        _cycleScrollView.autoScrollInterval = 3;
        _cycleScrollView.reloadDataNeedResetIndex = YES;
        [_cycleScrollView registerClass:[MBFooterBannerCell class] forCellWithReuseIdentifier:@"MBFooterBannerCell"];
    }
    return _cycleScrollView;
}

- (MBFooterPageView *)pageControlView {
    if (!_pageControlView) {
        _pageControlView = [MBFooterPageView new];
    }
    return _pageControlView;
}

- (UIView *)authorView {
    if (!_authorView) {
        _authorView = [UIView new];
        _authorView.backgroundColor = HEXS(@"EFEDFF");
        _authorView.layer.cornerRadius = MBHeightScale(9);
    }
    return _authorView;
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
@end
